/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/

package cn.com.dhc.danlu.activity.coupon;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashSet;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.adapter.BaseRecyclerviewAdapter;
import cn.com.dhc.danlu.widget.couponview.DanluHBView;
import cn.com.dhc.danlu.widget.couponview.DanluYHQView;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.dhc.danlu.widget.expandablelayout.ExpandableLayout;

/**
 * @author: 吴海洋
 * @author: 李一方
 * @version: 1.0.0
 * @since: 2016-09-23 17:44
 * @mail: wuhaiyang@danlu.com
 * @desc: 优惠券的 Adapter 基类
 */
abstract class BaseDealersCouponAdapter <VH extends BaseDealersCouponAdapter.BaseDealersCouponHolder> extends BaseRecyclerviewAdapter<VH> {
    final HashSet<Integer> mFoldingSet = new HashSet<>();
    protected Context context;
    FilterType mFilterType;

    BaseDealersCouponAdapter(Context context, FilterType type) {
        this.context = context;
        this.mFilterType = type;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public VH onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        if (mFilterType == FilterType.dealers_coupon) {
            //领券中心 |  我的优惠券
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dl_item_coupon_yhq, viewGroup, false);
        } else {
            // 抢经销商红包 | 使用经销商红包 、经销商红包
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dl_item_coupon_hb, viewGroup, false);
        }
        return createHolder(view);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        holder.refreshItemView(position);
    }

    protected abstract VH createHolder(View view);

    private void registerExpand(int position) {
        if (mFoldingSet.contains(position)) {
            removeExpand(position);
        } else {
            addExpand(position);
        }
    }

    private void removeExpand(int position) {
        mFoldingSet.remove(position);
    }

    private void addExpand(int position) {
        mFoldingSet.add(position);
    }

    abstract class BaseDealersCouponHolder extends XRecyclerView.ViewHolder {
        /**
         * 两个控件 不能共存
         */
        DanluYHQView dlYhq;
        DanluHBView dlEnvelop;

        BaseDealersCouponHolder(View itemView) {
            super(itemView);

            switch (mFilterType) {
                case dealers_coupon:
                    dlYhq = (DanluYHQView) itemView;
                    break;

                case dealers_envelop:
                case dealers_envelop_use:
                    dlEnvelop = (DanluHBView) itemView;
                    break;
            }
        }

        public abstract void refreshItemView(int pos);

        /**
         * 折叠展开 监听器
         */
        class CouponExpandListener implements ExpandableLayout.OnExpandListener {
            private int pos;

            CouponExpandListener(int pos) {
                this.pos = pos;
            }

            @Override
            public void onExpand(boolean expanded) {
                switch (mFilterType) {
                    case dealers_coupon:
                        dlYhq.setSelect(expanded);
                        break;

                    case dealers_envelop:
                    case dealers_envelop_use:
                        dlEnvelop.setSelect(expanded);
                        break;
                }

                registerExpand(pos);
            }
        }
    }
}